'''
20.有效的括号
'''
class Solution20:
    def isValid(self, s: str) -> bool:
        if not s:
            return True
        if len(s) == 1:
            return False

        # 定义字典存储括号
        dict1 = {')': '(', ']': '[', '}': '{'}

        # 定义堆
        stack = []

        for i in range(len(s)):
            s1 = s[i]
            # 在字典里说明是右括号
            if s1 in dict1.keys():
                # 栈为空，返回false
                if not stack:
                    return False
                # 判断栈顶是否为左括号
                right = stack.pop()
                if right != dict1.get(s1):
                    return False
            else:
                stack.append(s1)
        if stack:
            return False
        else:
            return True


if __name__ == '__main__':
    sl = Solution20()
    print(sl.isValid('(){}[]'))